﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>FormatTime - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The FormatTime function transforms a YYYYMMDDHH24MISS timestamp into the specified date/time format." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
<style type="text/css">
.red {color: #FF0000}
</style>
</head>
<body>

<h1>FormatTime</h1>

<p>Transforms a <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> timestamp into the specified date/time format.</p>

<pre class="Syntax">String := <span class="func">FormatTime</span>(<span class="optional"><a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a>, Format</span>)</pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>YYYYMMDD...</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>Leave this parameter blank to use the current local date and time. Otherwise, specify all or the leading part of a timestamp in the <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format.</p>
  </dd>

  <dt>Format</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>If omitted, it defaults to the time followed by the long date, both of which will be formatted according to the current user's locale. 例如: 4:55 PM Saturday, November 27, 2004</p>
    <p>Otherwise, specify one or more of the date-time formats from the tables below, along with any literal spaces and punctuation in between (commas do not need to be Escaped; they can be used normally). In the following example, note that M must be capitalized: M/d/yyyy h:mm tt</p>
  </dd>

</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
<p>This function returns the transformed version of the specified timestamp.</p>
<p>If <em>YYYYMMDDHH24MISS</em> contains a invalid date and/or time portion -- such as February 29th of a non-leap year -- the date and/or time will be omitted from the return value. Although only years between 1601 and 9999 are supported, a formatted time can still be produced for earlier years as long as the time portion is valid.</p>
<p>If <em>Format</em> contains more than 2000 characters, an empty string is returned.</p>

<h2>Date Formats (case sensitive)</h2>
<table class="info">
  <tr>
    <td style="width:15%">d</td>
    <td>Day of the month without leading zero (1 &ndash; 31)</td>
  </tr>
  <tr>
    <td>dd</td>
    <td>Day of the month with leading zero (01 &ndash; 31)</td>
  </tr>
  <tr>
    <td>ddd</td>
    <td>Abbreviated name for the day of the week (e.g. Mon) in the current user's language</td>
  </tr>
  <tr>
    <td>dddd</td>
    <td>Full name for the day of the week (e.g. Monday) in the current user's language</td>
  </tr>
  <tr>
    <td>M</td>
    <td>Month without leading zero (1 &ndash; 12)</td>
  </tr>
  <tr>
    <td>MM</td>
    <td>Month with leading zero (01 &ndash; 12)</td>
  </tr>
  <tr>
    <td>MMM</td>
    <td>Abbreviated month name (e.g. Jan) in the current user's language</td>
  </tr>
  <tr>
    <td>MMMM</td>
    <td>Full month name (e.g. January) in the current user's language</td>
  </tr>
  <tr>
    <td>y</td>
    <td>Year without century, without leading zero (0 &ndash; 99)</td>
  </tr>
  <tr>
    <td>yy</td>
    <td>Year without century, with leading zero (00 &ndash; 99)</td>
  </tr>
  <tr>
    <td>yyyy</td>
    <td>Year with century. 例如: 2005</td>
  </tr>
  <tr>
    <td>gg</td>
    <td>Period/era string for the current user's locale (blank if none)</td>
  </tr>
</table>
<h2>Time Formats (case sensitive)</h2>
<table class="info">
  <tr>
    <td style="width:15%">h</td>
    <td>Hours without leading zero; 12-hour format (1 &ndash; 12)</td>
  </tr>
  <tr>
    <td>hh</td>
    <td>Hours with leading zero; 12-hour format (01 &ndash; 12)</td>
  </tr>
  <tr>
    <td>H</td>
    <td>Hours without leading zero; 24-hour format (0 &ndash; 23)</td>
  </tr>
  <tr>
    <td>HH</td>
    <td>Hours with leading zero; 24-hour format (00 &ndash; 23)</td>
  </tr>
  <tr>
    <td>m</td>
    <td>Minutes without leading zero (0 &ndash; 59)</td>
  </tr>
  <tr>
    <td>mm</td>
    <td>Minutes with leading zero (00 &ndash; 59)</td>
  </tr>
  <tr>
    <td>s</td>
    <td>Seconds without leading zero (0 &ndash; 59)</td>
  </tr>
  <tr>
    <td>ss</td>
    <td>Seconds with leading zero (00 &ndash; 59)</td>
  </tr>
  <tr>
    <td>t</td>
    <td>Single character time marker, such as A or P (depends on locale)</td>
  </tr>
  <tr>
    <td>tt</td>
    <td>Multi-character time marker, such as AM or PM (depends on locale)</td>
  </tr>
</table>
<h2>Standalone Formats</h2>
  <p>The following formats must be used <strong>alone</strong>; that is, with no other formats or text present in the <em>Format</em> parameter. These formats are not case sensitive.</p>
<table class="info">
  <tr>
    <td style="width:15%">(Blank)</td>
    <td>Leave <em>Format</em> blank to produce the time followed by the long date. 例如, in some locales it might appear as 4:55 PM Saturday, November 27, 2004</td>
  </tr>
  <tr>
    <td>Time</td>
    <td>Time representation for the current user's locale, such as 5:26 PM</td>
  </tr>
  <tr>
    <td>ShortDate</td>
    <td>Short date representation for the current user's locale, such as 02/29/04</td>
  </tr>
  <tr>
    <td>LongDate</td>
    <td>Long date representation for the current user's locale, such as Friday, April 23, 2004</td>
  </tr>
  <tr>
    <td>YearMonth</td>
    <td>Year and month format for the current user's locale, such as February, 2004</td>
  </tr>
  <tr>
    <td>YDay</td>
    <td>Day of the year without leading zeros (1 &ndash; 366)</td>
  </tr>
  <tr>
    <td>YDay0</td>
    <td>Day of the year with leading zeros (001 &ndash; 366)</td>
  </tr>
  <tr>
    <td>WDay</td>
    <td>Day of the week (1 &ndash; 7).  Sunday is 1.</td>
  </tr>
  <tr>
    <td>YWeek</td>
    <td>The ISO 8601 full year and week number. 例如: 200453. If the week containing January 1st has four or more days in the new year, it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. Consequently, both January 4th and the first Thursday of January are always in week 1.</td>
  </tr>
</table>
<h2>Additional Options</h2>
<p>The following options can appear inside the <em>YYYYMMDDHH24MISS</em> parameter immediately after the timestamp (if there is no timestamp, they may be used alone). In the following example, note the lack of commas between the last four items:</p>
<pre>OutputVar := FormatTime("20040228 LSys D1 D4")</pre>
<p><strong>R</strong>: Reverse. Have the date come before the time (meaningful only when <em>Format</em> is blank).</p>
<p><strong>Ln</strong>: If this option is <em>not</em> present, the current user's locale is used to format the string. To use the system's locale instead, specify LSys. To use a specific locale, specify the letter L followed by a hexadecimal or decimal locale identifier (LCID). For information on how to construct an LCID, search <a href="http://www.microsoft.com">www.microsoft.com</a> for the following phrase: Locale Identifiers</p>
<p><strong>Dn</strong>: Date options. Specify for <strong>n</strong> one of the following numbers:<br>
  0: Force the default options to be used. This also causes the short date to be in effect. <br>
  1: Use short date (meaningful only when <em>Format</em> is blank; not compatible with 2 and 8).<br>
  2: Use long date (meaningful only when <em>Format</em> is blank; not compatible with 1 and 8).<br>
  4: Use alternate calendar (if any).<br>
  8: Use Year-Month format (meaningful only when <em>Format</em> is blank; not compatible with 1 and 2).<br>
  0x10: Add marks for left-to-right reading order layout.<br>
  0x20: Add marks for right-to-left reading order layout.<br>
  0x80000000: Do not obey any overrides the user may have in effect for the system's default date format. <br>
0x40000000: Use the system ANSI code page for string translation instead of the locale's code page.</p>
<p><strong>Tn</strong>: Time options. Specify for <strong>n</strong> one of the following numbers:<br>
  0: Force the default options to be used. This also causes minutes and seconds to be shown.<br>
  1: Omit minutes and seconds.<br>
  2: Omit seconds. <br>
  4: Omit time marker (e.g. AM/PM).<br>
  8: Always use 24-hour time rather than 12-hour time.<br>
  12: Combination of the above two.<br>
  0x80000000: Do not obey any overrides the user may have in effect for the system's default time format. <br>
  0x40000000: Use the system ANSI code page for string translation instead of the locale's code page.</p>
<p class="note"><strong>Note</strong>: Dn and Tn may be repeated to put more than one option into effect, such as this example: <code>FormatTime("20040228 D2 D4 T1 T8")</code></p>
<h2 id="Remarks">Remarks</h2>
<p>Letters and numbers that you want to be transcribed literally from <em>Format</em> into the final string should be enclosed in single quotes as in this example: <code>"'Date:' MM/dd/yy 'Time:' hh:mm:ss tt"</code>.</p>
<p>By contrast, non-alphanumeric characters such as spaces, tabs, linefeeds (`n), slashes, colons, commas, and other punctuation do not need to be enclosed in single quotes. The exception to this is the single quote character itself: to produce it literally, use four consecutive single quotes (''''), or just two if the quote is already inside an outer pair of quotes.</p>
<p>If <em>Format</em> contains date and time elements together, they must not be intermixed. In other words, the string should be dividable into two halves: a time half and a date half. 例如, a format string consisting of &quot;hh yyyy mm&quot; would not produce the expected result because it has a date element in between two time elements.</p>
<p>When <em>Format</em> contains a numeric day of the month (either d or dd) followed by the full month name (MMMM), the genitive form of the month name is used (if the language has a genitive form).</p>
<p>On a related note, addition, subtraction and comparison of dates and times can be performed with <a href="DateAdd.htm">DateAdd</a> and <a href="DateDiff.htm">DateDiff</a>.</p>
<h2 id="Related">相关</h2>
<p>To convert in the reverse direction -- that is, <em>from</em> a formatted date/time <em>to</em> <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format -- see <a href="https://www.autohotkey.com/forum/topic20405.html">www.autohotkey.com/forum/topic20405.html</a></p>
<p>See also: <a href="GuiControls.htm#DateTime">Gui DateTime control</a>, <a href="../Variables.htm#date">built-in date and time variables</a>, <a href="FileGetTime.htm">FileGetTime</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>TimeString := FormatTime()
MsgBox "The current time and date (time first) is " TimeString

TimeString := FormatTime("R")
MsgBox "The current time and date (date first) is " TimeString

TimeString := FormatTime(, "Time")
MsgBox "The current time is " TimeString

TimeString := FormatTime("T12", "Time")
MsgBox "The current 24-hour time is " TimeString

TimeString := FormatTime(, "LongDate")
MsgBox "The current date (long format) is " TimeString

TimeString := FormatTime(20050423220133, "dddd MMMM d, yyyy hh:mm:ss tt")
MsgBox "The specified date and time, when formatted, is " TimeString

MsgBox FormatTime(200504, "'Month Name': MMMM`n'Day Name': dddd")

YearWeek := FormatTime(20050101, "YWeek")
MsgBox "January 1st of 2005 is in the following ISO year and week number: " YearWeek</pre>
</div>

<div class="ex" id="ExFileTime">
<p><a href="#ExFileTime">#2</a>: 改变文件的日期-时间戳:</p>
<pre>FileName := FileSelect(3,, "Pick a file")
if FileName = "" <em>; 用户没有选取文件.</em>
    return
FileTime := FileGetTime(FileName)
FileTime := FormatTime(FileTime)   <em>; 由于省略了最后一个参数, 所以获取长日期和时间.</em>
MsgBox "The selected file was last modified at " FileTime</pre>
</div>

<div class="ex" id="ExFormatSec">
<p><a href="#ExFormatSec">#3</a>: 下面的函数把指定的秒数转换成相应的小时数, 分钟数和秒数(hh:mm:ss 格式).</p>
<pre>MsgBox FormatSeconds(7384)  <em>; 7384 = 2 小时 + 3 分钟 + 4 秒. 它的结果: 2:03:04</em>

FormatSeconds(NumberOfSeconds)  <em>; 把指定的秒数转换成 hh:mm:ss 格式.</em>
{
    time := 19990101  <em>; 任意日期的 *午夜*.</em>
    time := DateAdd(time, NumberOfSeconds, "Seconds")
    return NumberOfSeconds//3600 &quot;:&quot; FormatTime(time, "mm:ss")
    <em>/*
    ; 和上面方法不同的是, 这里不支持超过 24 小时的秒数:
    return FormatTime(time, "h:mm:ss")
    */</em>
}</pre>
</div>

</body>
</html>